import React from 'react';
import {parsePercent, toFixed2, safeMultiple} from '~/utils/number';
import CommonEchart from '../../../common/CommonEchart';

const NAMES = ['实际群效', '实际转化率'];

const tooltipCallback = (params = []) => {
    const tooltipList = params.map((chartItem, index) => {
        const {marker, name, seriesName, value} = chartItem;
        let formatValue = v => v;
        let result = '';
        if (seriesName === '实际群效') {
            formatValue = toFixed2 || formatValue;
        } else if (seriesName === '实际转化率') {
            formatValue = parsePercent || formatValue;
        }
        if (index === 0) {
            result = `${name}<br/>${marker}${seriesName}: ${formatValue(
                value
            )}`;
        } else {
            result = `<br/>${marker}${seriesName}: ${formatValue(value)}`;
        }
        return result;
    });
    return tooltipList.join('');
};

const floatLabelFormatter = (params = {}) => {
    const {value} = params;
    return toFixed2(value);
};

const percentLabelFormatter = (params = {}) => {
    const {value} = params;
    return parsePercent(value);
};

const extendOptions = {
    chartId: 'TransformLineChart',
    // title: {
    //     text: '某站点用户访问来源',
    //     subtext: '纯属虚构',
    //     left: 'center'
    // },
    tooltip: {
        show: true,
        trigger: 'axis',
        formatter: tooltipCallback
    },
    legend: {
        // orient: 'vertical',
        left: 'left'
    },
    grid: {
        show: false,
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    yAxis: {
        newYAxis: [
            {type: 'value', name: ''},
            {
                type: 'value',
                name: '',
                axisLabel: {formatter: value => `${safeMultiple(value, 100)}%`}
            }
        ],
        common: {
            splitLine: {show: false}
        }
    },
    series: {
        newYAxis: [
            {
                yAxisIndex: 0,
                name: '实际群效',
                // label: {
                //     show: true,
                //     position: 'top',
                //     formatter: floatLabelFormatter
                // }
            },
            {
                yAxisIndex: 1,
                name: '实际转化率',
                // label: {
                //     show: true,
                //     position: 'top',
                //     formatter: percentLabelFormatter
                // }
            }
        ],
        common: {
            emphasis: {
                focus: 'series'
            }
        }
    }
};

const TransformLineChart = props => (
    <CommonEchart {...props} extendEchartsOptions={extendOptions} />
);

export default TransformLineChart;
